/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.service.clusterhandler;

import com.asiainfo.redis.entity.ClusterNode;
import com.asiainfo.redis.repository.ClusterNodeRepository;
import com.asiainfo.redis.service.HostService;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisClusterNode;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @version v 1.0 on 2016/5/19 16:40
 * @auther william.xu
 */
public class RemoveSlaveNodes {

    private RedisClusterHandler redisClusterHandler;
    private HostService hostService;
    private ClusterNodeRepository clusterNodeRepository;

    private Long clusterId;
    private List<RedisClusterNode> slaveList;
    private List<RedisClusterNode> masterList;

    public RemoveSlaveNodes(RedisClusterHandler redisClusterHandler, HostService hostService, ClusterNodeRepository
            clusterNodeRepository, Long clusterId, List<RedisClusterNode> slaveList, List<RedisClusterNode>
            masterList) {
        this.redisClusterHandler = redisClusterHandler;
        this.hostService = hostService;
        this.clusterNodeRepository = clusterNodeRepository;
        this.clusterId = clusterId;
        this.slaveList = slaveList;
        this.masterList = masterList;
    }

    public Map<RedisClusterNode, Collection<RedisClusterNode>> doInRedisCluster(RedisClusterConnection connection)
            throws Exception {
        redisClusterHandler.checkClusterNodes(connection);
        //保留原来的主从关系数据
        Map<RedisClusterNode, Collection<RedisClusterNode>> masterSlaveMap = connection
                .clusterGetMasterSlaveMap();
        Iterator<RedisClusterNode> iterator = masterSlaveMap.keySet().iterator();
        while (iterator.hasNext()) {
            RedisClusterNode master = iterator.next();//原来的master，新的cluster中的slave
            Collection<RedisClusterNode> slaves = masterSlaveMap.get(master);
            if (slaves.size() > 1) {
                throw new Exception("该集群存在一个主节点对应多个从节点的情况，不能执行这样的切换操作");
            }
            if (slaves.size() == 0) {
                throw new Exception("该集群存在主节点没有对应从节点的情况，不能执行这样的切换操作");
            }
        }

        //把slave节点从原来集群中移走
        Iterator<RedisClusterNode> it = connection.clusterGetNodes().iterator();
        while (it.hasNext()) {
            RedisClusterNode redisClusterNode = it.next();
            if (redisClusterNode.isSlave()) {
                slaveList.add(redisClusterNode);
                redisClusterHandler.forgetNode(connection, redisClusterNode);
                List<ClusterNode> list = clusterNodeRepository.findByClusterIdAndIpAndPort(clusterId,
                        redisClusterNode.getHost(), redisClusterNode.getPort());
                if (list.size() > 0) {
                    hostService.deleteNodesConfigFile(list.get(0));
                }
            } else {
                masterList.add(redisClusterNode);
            }
        }
        return masterSlaveMap;
    }
}
